﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using Ivy.UserRegistration.Entity;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;

using Ivy.Common.Framework.GeneralUtility;


namespace DasIdeen.UserControls
{
    public partial class ProjectMemberRegisterControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        //All constants and local variable should be declared here
        ProjectMemberMaster lProjectMemberMaster = null;
        int lProjectMemberKey = -1;
        const string constCultureInfo = "CultureInfo";
        #endregion  Variable & constant

        #region Constructor & Destructor
        //Generally not needed in UI layer
        #endregion Constructor & Destructor

        #region Properties
        //UI layer may have public custom properties. these are very handy for User Controls
        public ProjectMemberMaster ProjectMemberMasterEntity
        {
            get
            {
                return lProjectMemberMaster;
            }
            set
            {
                lProjectMemberMaster = value;
                this.lProjectMemberKey = lProjectMemberMaster.ProjectMemberKey;
            }

        }

        public Int32 ProjectMemberKey
        {
            get
            {
                return lProjectMemberKey;
            }
            set
            {
                lProjectMemberKey = value;
            }
        }
        #endregion Properties

        #region Public Methods
        //UI layer may have public custom properties. these are very handy for User Controls
        public override void DataBind()
        {
            try
            {
                if (this.ProjectMemberMasterEntity != null)
                {
                    TranslateEntityToUIControls(this.ProjectMemberMasterEntity);
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }

        }

        public void DataBind(ProjectMemberMaster pProjectMemberMaster)
        {
            try
            {
                if (pProjectMemberMaster != null)
                {
                    this.ProjectMemberMasterEntity = pProjectMemberMaster;
                    this.DataBind();
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        public ProjectMemberMaster GetSelectedProjectMember(int ProjectMemberKey)
        {
            this.lProjectMemberKey = ProjectMemberKey;
            ProjectMemberMaster lProjectMemberMaster;
            UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
            lProjectMemberMaster = lUserRegistrationBus.GetProjectMemberDetailsByKey(this.lProjectMemberKey);

            return lProjectMemberMaster;
        }
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try //All events in pages or user control should have try{} catch {} block.
            {
                //First check if the current logged in user is having proper role/authorization to access this page or not.
                if (IsUserAutherization())
                {
                    //TODO - Your code to display the initial layout/data etc.
                }
                else
                {
                    //TODO - Your code to redirect page to "NOT AUTHORIZED" page or any other page as required by the business logic.
                }
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        protected void btnProjectMember_Click(object sender, EventArgs e)
        {
            try
            {

                ProjectMemberMaster lProjectMemberMaster = new ProjectMemberMaster();
                UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();

                if (lProjectMemberKey != -1)
                {
                    lProjectMemberMaster = lUserRegistrationBus.GetProjectMemberDetailsByKey(this.lProjectMemberKey);
                    TranslateUIControlsToEntity(lProjectMemberMaster);
                    lUserRegistrationBus.UpdateProjectMemberDetails(lProjectMemberMaster);
                    this.DivProjectMemberRegister.Visible = false;
                }
                else
                {
                    TranslateUIControlsToEntity(lProjectMemberMaster);
                    lUserRegistrationBus.SaveProjectMemberDetails(lProjectMemberMaster);
                    this.DivProjectMemberRegister.Visible = false;
                }
                Response.Redirect("~/Pages/ProjectMemberCommunicationList.aspx");
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
            
        }

        protected void btnEditProjectMemberDetails_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                this.DivEditProjectMember.Visible = false;
                ProjectMemberMaster lProjectMemberMaster;
                UserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                lProjectMemberMaster = lUserRegistrationBus.GetProjectMemberDetailsByKey(this.lProjectMemberKey);
                TranslateEntityToUIControls(lProjectMemberMaster);
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }

        }

        #endregion Events

        #region Private Methods

        /// <summary>
        /// This method is to check if the current logged in user is having proper role/authorization to access this page or not.
        /// </summary>
        private bool IsUserAutherization()
        {
            bool _IsUserAutherization = false;
            //TODO - Your code to check authorization goes here.

            //for the time being let us allow the user to access this page.
            //but remove the below line once you have a proper logic.
            _IsUserAutherization = true;

            return _IsUserAutherization;
        }

        /// <summary>
        /// This method is to clear all UI controls which are data binded.
        /// </summary>
        private void ClearScreen()
        {
        }

        /// <summary>
        /// This method is to bind all the UI control like dropdown list or list box etc with the required initial data, 
        /// </summary>
        private void FillLoadLookUpData()
        {
        }

        /// <summary>
        /// This method is to load the UI with initial set of data based of the business logic.
        /// </summary>
        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        /// <summary>
        /// This method is to copy the entity properties that need to be displayed in the UI controls to the UI control.
        /// Example - [pUser] is an instance of an entity and is filled with specific USER data.
        /// and this user instance is to be displayed is an UI control text boxes.
        /// A sample code will look like the following:
        ///  this.txtFirstName.Text = pUser.UserFirstName; 
        ///  this.txtLastName.Text  = pUser.UserLastName; 
        ///  this.txtLoginId.Text   = pUser.UserLoginID;
        /// </summary>
        private void TranslateEntityToUIControls(ProjectMemberMaster pProjectMemberMaster)
        {
            this.txtFirstName.Text = pProjectMemberMaster.ProjectMemberFirstName;
            this.txtLastName.Text = pProjectMemberMaster.ProjectMemberLastName;
            this.txtPrimaryEmailId.Text = pProjectMemberMaster.ProjectMemberPrimaryEmailID;
            this.txtCodePlexId.Text = pProjectMemberMaster.ProjectMemberCodePlexID;
            this.txtSkypeId.Text = pProjectMemberMaster.ProjectMemberSkypeID;
            this.txtMicrosoftId.Text = pProjectMemberMaster.ProjectMemberMicrosoftID;
            this.txtGoogleId.Text = pProjectMemberMaster.ProjectMemberGoogleID;
            this.txtTimeZoonReference.Text = pProjectMemberMaster.ProjectMemberTimeZoneReference;
            this.txtTimeZoonName.Text = pProjectMemberMaster.ProjectMemberTimeZoneName;
            this.txtTimeZoonAbbreviation.Text = pProjectMemberMaster.ProjectMemberTimeZoneAbbreviation;
            this.txtCountryName.Text = pProjectMemberMaster.ProjectMemberCountryName;
            this.txtCityName.Text = pProjectMemberMaster.ProjectMemberCityName;
            this.txtDateJoining.Text = pProjectMemberMaster.ProjectMemberDateOfJoining.ToString();
            this.chkIsMemberOfFacebook.Checked = pProjectMemberMaster.ProjectMemberIsMemberOfFacebook;
            if (chkIsMemberOfFacebook.Checked == true)
            {
                this.chkIsMemberOfFacebook.Checked = pProjectMemberMaster.ProjectMemberIsMemberOfFacebook;
            }
        }

        /// <summary>
        /// This method is to copy the user entered data from the UI controls to the entity instance that will be used to sent the entity to any other layer.
        /// Example - [pUser] is an instance of an entity and is to be filled with specific USER data that has been captured in the screen.
        /// A sample code will look like the following:
        ///  pUser.UserFirstName = this.txtFirstName.Text; 
        ///  pUser.UserLastName  = this.txtLastName.Text; 
        ///  pUser.UserLoginID   = this.txtLoginId.Text; 
        /// </summary>
        private void TranslateUIControlsToEntity(ProjectMemberMaster pProjectMemberMaster)
        {
            pProjectMemberMaster.ProjectMemberUserKey = -1;
            pProjectMemberMaster.ProjectMemberFirstName = this.txtFirstName.Text;
            pProjectMemberMaster.ProjectMemberLastName = this.txtLastName.Text;
            pProjectMemberMaster.ProjectMemberPrimaryEmailID = this.txtPrimaryEmailId.Text;
            pProjectMemberMaster.ProjectMemberCodePlexID = this.txtCodePlexId.Text;
            pProjectMemberMaster.ProjectMemberSkypeID = this.txtSkypeId.Text;
            pProjectMemberMaster.ProjectMemberMicrosoftID = this.txtMicrosoftId.Text;
            pProjectMemberMaster.ProjectMemberGoogleID = this.txtGoogleId.Text;
            pProjectMemberMaster.ProjectMemberTimeZoneReference = this.txtTimeZoonReference.Text;
            pProjectMemberMaster.ProjectMemberTimeZoneName = this.txtTimeZoonName.Text;
            pProjectMemberMaster.ProjectMemberTimeZoneAbbreviation = this.txtTimeZoonAbbreviation.Text;
            pProjectMemberMaster.ProjectMemberCountryName = this.txtCountryName.Text;
            pProjectMemberMaster.ProjectMemberCityName = this.txtCityName.Text;

            AppSettingsReader _AppSettingsReader = new AppSettingsReader();
            string _CultureInfo = _AppSettingsReader.GetValue(constCultureInfo, typeof(string)).ToString();

            IFormatProvider _CultureFormatProvider = new System.Globalization.CultureInfo(_CultureInfo, true);
            if (!string.IsNullOrEmpty(txtDateJoining.Text.Trim()))
            {
                pProjectMemberMaster.ProjectMemberDateOfJoining = DateTime.Parse(txtDateJoining.Text.Trim(), _CultureFormatProvider, System.Globalization.DateTimeStyles.AssumeLocal);
            }
            else
            {
                pProjectMemberMaster.ProjectMemberDateOfJoining = null;
            }
            if (chkIsMemberOfFacebook.Checked == true)
            {
                pProjectMemberMaster.ProjectMemberIsMemberOfFacebook = true;
            }
            else
            {
                pProjectMemberMaster.ProjectMemberIsMemberOfFacebook = false;
            }
            pProjectMemberMaster.Sys_CreatedByID = "-1";
        }

        #endregion Private Methods
    }
}